package com.activitiFlow.service.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.activitiFlow.service.IRepositoryBiz;

@Service("repositoryBiz")
public class RepositoryBizImpl implements IRepositoryBiz {

	@Autowired
	private RepositoryService repositoryService;
	
	@Autowired
	private TaskService taskService;
	
	@Override
	public void saveNewDeploy(String filename, InputStream fileStream) {
		ZipInputStream zipIS = new ZipInputStream(fileStream);
		repositoryService.createDeployment().name(filename).addZipInputStream(zipIS).deploy();
	}

	@Override
	public List<Deployment> queryDeploymentList() {
		List<Deployment> list=repositoryService.createDeploymentQuery().orderByDeploymenTime().desc().list();
		return list;
	}
	
	@Override
	public List<ProcessDefinition> queryProcessDefinitionList() {
		List<ProcessDefinition>  list=repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionVersion().asc().list();
		Map<String,ProcessDefinition> map=new HashMap<String, ProcessDefinition>();
		if(list!=null && list.size()>0){
			for (ProcessDefinition processDefinition : list) {
				map.put(processDefinition.getKey(), processDefinition);
			}
		}
		List<ProcessDefinition>  pdList=new ArrayList<ProcessDefinition>(map.values());
		return pdList;
	}
	
	
	@Override
	public Map<String, String> getProcessDefinitionKeyName(){
		Map<String,String> processDefinitionKeyName=new HashMap<String,String>();
		List<ProcessDefinition> processDefinitionList= queryProcessDefinitionList();
		if(processDefinitionList!=null && processDefinitionList.size()>0){
			for (ProcessDefinition processDefinition : processDefinitionList) {
				Deployment deployment=queryDeploymentById(processDefinition.getDeploymentId());
				processDefinitionKeyName.put(processDefinition.getKey(),deployment.getName());
			}
		}
		return processDefinitionKeyName;
	}
	
	
	@Override
	public Deployment queryDeploymentById(String deploymentId){
		return repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult();
	}
	
	@Override
	public InputStream findDeployImg(String deploymentId, String imageName) {
		return repositoryService.getResourceAsStream(deploymentId, imageName);
	}
	
	@Override
	public void deleteDeploy(String deploymentId) {
		repositoryService.deleteDeployment(deploymentId,true);
	}
	
	@Override
	public ProcessDefinition getProcessDefinitionById(String processDefinitionId) {
		return repositoryService.getProcessDefinition(processDefinitionId);
	}
	
	@Override
	public ProcessDefinition queryProcessDefinitionByTaskId(String taskId) {
		return repositoryService.getProcessDefinition(taskService.createTaskQuery().taskId(taskId).singleResult().getProcessDefinitionId());
//		return repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
	}
}
